﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BlogDH
{
    public partial class Login : System.Web.UI.Page
    {
        protected string errorStr = string.Empty;
        protected string username = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                username = CommMethod.GetCookieValue("uname");
            }

            if (Request.HttpMethod == "POST")
            {

                username = Request.Form["uname"];
                string pwd = Request.Form["pwd"];

                string reg = "[0-9a-zA-Z]+[0-9a-zA-Z\\.-]*\\.[a-zA-Z]{2,4}";
                if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(pwd) || username.Length > 50 || pwd.Length > 20 || !System.Text.RegularExpressions.Regex.IsMatch(username, reg))
                {
                    errorStr = "信息有误，请重新填写！";
                }
                else
                {
                    BLL.UserInfo bll = new BLL.UserInfo();
                    Model.UserInfo model = bll.GetModel(username);

                    if (model == null)
                    {
                        errorStr = "博客信息不存在！";
                    }
                    else if (model.PWD != CommMethod.MD5Hash(pwd))
                    {
                        errorStr = "密码错误！";
                    }
                    else
                    {
                        CommMethod.SetCookieValue("u", model.UserName, System.DateTime.Now.AddDays(7));
                        CommMethod.SetCookieValue("t", model.UserID, System.DateTime.Now.AddDays(7));

                        Response.Redirect("/user/index.aspx");
                    }
                }
            }
        }
    }
}